const app = getApp();
const pageSize = 20;
const statusList = [{
  value: "",
  name: '全部'
}, {
  value: 1,
  name: '用户充值'
}, {
  value: 2,
  name: '合伙人充值'
}, {
  value: 3,
  name: '平台'
}, {
  value: 4,
  name: '加盟商'
}, {
  value: 5,
  name: '销售'
}];
const orderTypes = [{
  value: "",
  name: "全部"
}, {
  value: 1,
  name: "充值订单"
}, {
  value: 3,
  name: "赠送订单"
}, {
  value: 4,
  name: "促销项目"
}];

Page({
  /**
   * 页面的初始数据
   */
  data: {
    empty: false,
    total: "",
    give_money: "",
    recharge_money: "",
    record_data: [],
    page: 1,
    loadmore: false,
    nomore: false,
    drawer: false,
    isFilter: false,
    statusList,
    conditions: {
      start_time: '',
      end_time: '',
      statusIndex: "",
      card: '',
      orderTypeIndex: "",
      village_name: ''
    },
    orderTypes,
    isSpecial: false,
    isPromoter: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let is_special = options.is_special == 1 ? true : false || false;
    this.setData({
      isSpecial: is_special,
      isPromoter: options.is_promoter || false,
    })
    let title = `充值记录${!this.data.isPromoter?'(普通水卡)':''}`;
    if (is_special) {
      title = "充值记录(特殊水卡)";
    }
    wx.setNavigationBarTitle({
      title,
    })

    this.card_recharge_record_mobile();
  },

  onPullDownRefresh: function () {
    this.reloadData();
  },

  onReachBottom: function () {
    if (this.data.loadmore) {
      this.setData({
        page: this.data.page + 1
      });
      this.card_recharge_record_mobile();
    }
  },

  reloadData() {
    this.setData({
      record_data: [],
      page: 1,
      loadmore: false,
      nomore: false,
    })
    wx.showLoading();
    this.card_recharge_record_mobile();
  },

  card_recharge_record_mobile() {
    const param = {
      page: this.data.page,
      row: pageSize,
      recharge_source: this.data.conditions.statusIndex !== "" ? this.data.statusList[this.data.conditions.statusIndex].value : "",
      start_time: this.data.conditions.start_time,
      end_time: this.data.conditions.end_time,
      card_number: this.data.conditions.card,
      village_name: this.data.conditions.village_name
    };
    if (this.data.isSpecial) {
      param.order_type = 2;
    } else {
      param.order_type = this.data.conditions.orderTypeIndex !== "" ? this.data.orderTypes[this.data.conditions.orderTypeIndex].value : ""
    }
    if (this.data.isPromoter) {
      param.salesman_id = wx.getStorageSync('salesman_id');
    }
    app.ajax({
      url: 'Partner/WaterCard/card_recharge_record_mobile',
      method: "POST",
      data: param,
      success: res => {
        wx.hideLoading();
        wx.stopPullDownRefresh();
        if (res.data.code == 1000) {
          const source = res.data.data;
          const list = this.data.record_data;
          let nList = [];
          if (source.data.length > 0) {
            source.data.forEach(item => {
              nList.push(...item.child);
            })
          }

          let lists = list.concat(nList);
          // if (source.data.length > 0 && list.length > 0) {
          //   if (list[list.length - 1].date == source.data[0].date) {
          //     let last = list[list.length - 1].child.concat(source.data[0].child);
          //     list.pop();
          //     source.data[0].child = last;
          //   }
          // }
          // let lists = list.concat(source.data);
          // let len = 0;
          // lists.map(current => {
          //   len += current.child.length
          // })
          this.setData({
            total: source.census,
            give_money: source.give_money,
            recharge_money: source.recharge_money,
            drawer: false,
            loadmore: lists.length < source.page.total_number ? true : false,
            nomore: lists.length >= source.page.total_number ? true : false,
            record_data: lists,
            empty: source.page.total_number == 0 ? true : false,
          })
        } else {
          app.showToast(res.data.message);
        }
      },
      error: e => {
        wx.hideLoading();
        wx.stopPullDownRefresh();
        app.showToast("网络错误");
      }
    })
  },

  //查看省略文字
  looktxt(e) {
    app.showToast(e.currentTarget.dataset.txt);
  },

  handleTime(e) {
    const name = e.currentTarget.dataset.name;
    this.setData({
      [`conditions.${name}`]: e.detail.value
    })
  },


  handleChange(e) {
    const {
      index,
      name
    } = e.currentTarget.dataset;
    this.setData({
      [`conditions.${name}`]: index
    })
  },



  showDrawer() {
    this.condition = {
      ...this.data.conditions
    };
    this.setData({
      drawer: true,
    })
  },

  closeDrawer() {
    this.setData({
      drawer: false,
      conditions: this.condition
    })
    delete(this.condition)
  },

  resetDrawer() {
    const conditions = this.data.conditions;
    for (let x in conditions) {
      conditions[x] = "";
    }
    this.setData({
      conditions,
    })
  },

  confirmDrawer() {
    let isFilter = false;
    const conditions = this.data.conditions;
    for (let x in conditions) {
      conditions[x] !== "" && (isFilter = true);
    }
    this.setData({
      isFilter
    })
    delete(this.condition);
    this.reloadData();
  },

})